home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / Mesa-3.0 / BeOS / TKBEOS.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1998-07-25  |  21.2 KB  |  1,067 lines

  1. /*
  2.  * (c) Copyright 1993, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED
  4.  * Permission to use, copy, modify, and distribute this software for
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  *
  25.  * US Government Users Restricted Rights
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36.  */
  37.  
  38. // Mesa Tweaking by: Mark E. Peterson (markp@ic.mankato.mn.us)
  39.  
  40. #include <KernelKit.h>
  41. #include <AppKit.h>
  42. #include <MessageFilter.h>
  43. #include <InterfaceKit.h>
  44. #include <StorageKit.h>
  45. #include <SupportKit.h>
  46. #include <unistd.h>
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #include "gltk.h"
  51. #ifdef FX
  52. #include "GL/fxmesa.h"
  53. #else /* FX */
  54. #include "GL/osmesa.h"
  55. #endif /* FX */
  56.  
  57. static struct _WINDOWINFO {
  58.     int x, y;
  59.     int width, height;
  60.     GLenum type;
  61.     GLenum dmPolicy;
  62.     int ipfd;
  63.     bool bDefPos;
  64.     char *title;
  65. } windInfo = {
  66.     50, 30, 320, 200, (GLenum)(TK_RGB | TK_SINGLE), (GLenum)0, 0, TRUE, "TKView"
  67. };
  68.  
  69. static void (*ExposeFunc)(int, int)              = NULL;
  70. static void (*ReshapeFunc)(GLsizei, GLsizei)     = NULL;
  71. static void (*DisplayFunc)(void)                 = NULL;
  72. static GLenum (*KeyDownFunc)(int, GLenum)        = NULL;
  73. static GLenum (*MouseDownFunc)(int, int, GLenum) = NULL;
  74. static GLenum (*MouseUpFunc)(int, int, GLenum)   = NULL;
  75. static GLenum (*MouseMoveFunc)(int, int, GLenum) = NULL;
  76. static void (*IdleFunc)(void)                    = NULL;
  77.  
  78. float tkRGBMap[8][3] = {
  79.     {
  80.     0, 0, 1
  81.     },
  82.     {
  83.     1, 0, 1
  84.     },
  85.     {
  86.     0, 1, 1
  87.     },
  88.     {
  89.     1, 1, 1
  90.     },
  91.     {
  92.     1, 1, 0
  93.     },
  94.     {
  95.     0, 1, 0
  96.     },
  97.     {
  98.     1, 0, 0    
  99.     },
  100.     {
  101.     0, 0, 0
  102.     }
  103. }; 
  104.  
  105.  
  106. static uint8 idxlist[256];
  107. #ifndef FX
  108. static uchar *tmpout;
  109. static uchar *bits;
  110. #endif /* not FX */
  111.  
  112. /***************************************************************
  113.  *                                                             *
  114.  *  BeOS stuff                                                 *
  115.  *                                                             *
  116.  ***************************************************************/
  117. const     ulong            MSG_REDRAW = 1;
  118. char                    *name;
  119.  
  120. class                     MesaWindow;
  121. class                     MesaView;
  122. class                    MesaApp;
  123.  
  124.  
  125. // Lets make our life easy and make them global:
  126.  
  127. #ifndef FX
  128. OSMesaContext             ctx;    
  129. BBitmap                 *the_bitmap;
  130. #else /* FX */
  131. fxMesaContext            ctx;
  132. #endif /* FX */
  133. MesaView                *the_view;    
  134. MesaWindow                 *the_window;
  135. MesaApp                 *the_app;
  136. int                        x_pos, y_pos;
  137.  
  138. class MesatkMessageFilter : public BMessageFilter {
  139.     public:
  140.         MesatkMessageFilter(message_delivery delivery,message_source source) 
  141.                             : BMessageFilter(delivery,source)
  142.         {
  143.             //    ignore first B_MOUSE_UP event on first click
  144.             mouse_status = false;
  145.         }
  146.  
  147.         virtual    filter_result Filter(BMessage *message, BHandler **target) 
  148.         {
  149.         // NOTE: This Filter should _ONLY_ be used with BView objects
  150.         BView *view = (BView *)*target;
  151.  
  152.         switch(message->what) {
  153.             case B_MOUSE_DOWN:
  154.                 //    not first click so we dont ignore B_MOUSE_UP
  155.                 mouse_status = true;
  156.                 //    MAGICK!!
  157.                 //        we got a mouse down message so lets pass it
  158.                 //        to the BView that instanced this filter
  159.                 view->MouseDown(message->FindPoint("where"));
  160.                 //    skip this message because we handed it off
  161.                 return B_SKIP_MESSAGE;
  162.                 break;
  163.             case B_MOUSE_UP:
  164.                 //    if not first click dont ignore B_MOUSE_UP
  165.                 if (mouse_status == true) {
  166.                     //    MAGICK!!
  167.                     //        we got a mouse up message so lets pass it
  168.                     //        to the BView that instanced this filter
  169.                     view->MouseUp(message->FindPoint("where"));
  170.                     //    reset mouse status
  171.                     mouse_status = false;
  172.                 }
  173.                 //    skip this message because we handed it off
  174.                 return B_SKIP_MESSAGE;
  175.                 break;
  176.             default:
  177.                 //    dispatch this message because we have no clue
  178.                 //    or dont care what it is...
  179.                 return B_DISPATCH_MESSAGE;
  180.                 break;
  181.         }
  182.     }
  183.  
  184.     private:
  185.         bool mouse_status;
  186. };
  187.  
  188.  
  189. class MesaView : public BView
  190. {
  191.     MesatkMessageFilter    *lmf;
  192.     
  193.     public:
  194.     
  195.     BRect     Bound;
  196.     
  197.     MesaView(BRect frame):BView(frame,"Mesa View",B_FOLLOW_NONE, B_WILL_DRAW) 
  198.     {
  199.         Bound = Window()->Bounds();
  200.     };
  201.     
  202.     ~MesaView()
  203.     {
  204.         return;
  205.     };
  206.  
  207.     void AttachedToWindow()
  208.     {
  209.         MakeFocus();
  210.  
  211.         //    MAGICK!!
  212.         BList *foobaz;
  213.         lmf = new MesatkMessageFilter(B_ANY_DELIVERY,B_ANY_SOURCE);
  214.         AddFilter(lmf);
  215.         foobaz = FilterList();
  216.  
  217.         if (foobaz == NULL) fprintf(stderr, "Filter Pointer NULL!!");
  218.     
  219.     };
  220.     
  221.  
  222.     void MouseMoved(BPoint pos, uint32 transit, const BMessage *msg)
  223.     {
  224.         ulong buttons = NULL;
  225.         ulong mask;
  226.         
  227.         if (Window()->IsFront())
  228.         {
  229.             x_pos = (int)pos.x;
  230.             y_pos = (int)pos.y;
  231.         }
  232.         
  233.         if(MouseMoveFunc)
  234.         {
  235.             mask = 0;
  236.             if (buttons & B_PRIMARY_MOUSE_BUTTON) {
  237.                    mask |= TK_LEFTBUTTON;
  238.             }
  239.             if (buttons & B_SECONDARY_MOUSE_BUTTON) {
  240.                    mask |= TK_MIDDLEBUTTON;
  241.             }
  242.             if (buttons & B_TERTIARY_MOUSE_BUTTON) {
  243.                    mask |= TK_RIGHTBUTTON;
  244.             }
  245.             (*MouseUpFunc)(x_pos,y_pos,(GLenum)mask);
  246.             if( DisplayFunc)
  247.             {
  248.                 (*DisplayFunc)();
  249.                     Draw(Bound);
  250.             }
  251.         }
  252.         
  253.         BView::MouseMoved(pos, transit, msg);
  254.         
  255.         return;
  256.     }    
  257.  
  258.     void MouseDown(BPoint pos)
  259.     {
  260.         uint32 buttons = NULL;
  261.         ulong mask;
  262.  
  263.         MakeFocus();
  264.         GetMouse(&pos,&buttons);
  265.         x_pos = (int)pos.x;
  266.         y_pos = (int)pos.y;
  267.  
  268.         if(MouseDownFunc)
  269.         {
  270.  
  271.             mask = 0;
  272.             if (buttons & B_PRIMARY_MOUSE_BUTTON) {
  273.                 mask |= TK_LEFTBUTTON;
  274.             }
  275.             if (buttons & B_SECONDARY_MOUSE_BUTTON) {
  276.                 mask |= TK_MIDDLEBUTTON;
  277.             }
  278.             if (buttons & B_TERTIARY_MOUSE_BUTTON) {
  279.                 mask |= TK_RIGHTBUTTON;
  280.             }
  281.  
  282.             (*MouseDownFunc)(x_pos,y_pos,(GLenum)mask);
  283.             if( DisplayFunc)
  284.             {
  285.                 (*DisplayFunc)();
  286.                     Draw(Bound);
  287.             }
  288.         }
  289.  
  290.     };
  291.  
  292.  
  293.     void MouseUp(BPoint pos)
  294.     {
  295.         BView::MouseUp(pos);
  296.  
  297.         uint32 buttons = NULL;
  298.         ulong mask;
  299.  
  300.         MakeFocus();
  301.         GetMouse(&pos,&buttons);
  302.         x_pos = (int)pos.x;
  303.         y_pos = (int)pos.y;
  304.         
  305.         if(MouseUpFunc)
  306.         {
  307.             mask = 0;
  308.             if (buttons & B_PRIMARY_MOUSE_BUTTON) {
  309.                 mask |= TK_LEFTBUTTON;
  310.             }
  311.             if (buttons & B_SECONDARY_MOUSE_BUTTON) {
  312.                 mask |= TK_MIDDLEBUTTON;
  313.             }
  314.             if (buttons & B_TERTIARY_MOUSE_BUTTON) {
  315.                 mask |= TK_RIGHTBUTTON;
  316.             }
  317.  
  318.             (*MouseUpFunc)(x_pos,y_pos,(GLenum)mask);
  319.             if( DisplayFunc)
  320.             {
  321.                 (*DisplayFunc)();
  322.                     Draw(Bound);
  323.             }
  324.         }
  325.  
  326.         return;
  327.     };
  328.     
  329.     void KeyDown(const char *bytes, int32 numBytes)
  330.     {
  331.         int key;
  332.         GLenum mask=(GLenum)0;
  333.         BMessage *msg = Window()->CurrentMessage();
  334.         
  335.         if(numBytes == 1)
  336.         {
  337.             switch(bytes[0])
  338.             {
  339.                 case B_UP_ARROW:
  340.                         key = TK_UP;
  341.                         break;
  342.                 case B_DOWN_ARROW:
  343.                         key = TK_DOWN;
  344.                         break;
  345.                 case B_RIGHT_ARROW:
  346.                         key = TK_RIGHT;
  347.                         break;
  348.                 case B_LEFT_ARROW:
  349.                         key = TK_LEFT;
  350.                         break;
  351.                 case B_SPACE:
  352.                         key = TK_SPACE;
  353.                         break;
  354.                 case B_ESCAPE:
  355.                         key = TK_ESCAPE;
  356.                         break;
  357.                 case '1':
  358.                         key = TK_1;
  359.                         break;
  360.                 case '2':
  361.                         key = TK_2;
  362.                         break;
  363.                 case '3':
  364.                         key = TK_3;
  365.                         break;
  366.                 case '4':
  367.                         key = TK_4;
  368.                         break;
  369.                 case '5':
  370.                         key = TK_5;
  371.                         break;
  372.                 case '6':
  373.                         key = TK_6;
  374.                         break;
  375.                 case '7':
  376.                         key = TK_7;
  377.                         break;
  378.                 case '8':
  379.                         key = TK_8;
  380.                         break;
  381.                 case '9':
  382.                         key = TK_9;
  383.                         break;
  384.                 case '0':
  385.                         key = TK_0;
  386.                         break;
  387.                 case 'a':
  388.                         key = TK_a;
  389.                         break;
  390.                 case 'b':
  391.                         key = TK_b;
  392.                         break;
  393.                 case 'c':
  394.                         key = TK_c;
  395.                         break;
  396.                 case 'd':
  397.                         key = TK_d;
  398.                         break;
  399.                 case 'e':
  400.                         key = TK_e;
  401.                         break;
  402.                 case 'f':
  403.                         key = TK_f;
  404.                         break;
  405.                 case 'g':
  406.                         key = TK_g;
  407.                         break;
  408.                 case 'h':
  409.                         key = TK_h;
  410.                         break;
  411.                 case 'i':
  412.                         key = TK_i;
  413.                         break;
  414.                 case 'j':
  415.                         key = TK_j;
  416.                         break;
  417.                 case 'k':
  418.                         key = TK_k;
  419.                         break;
  420.                 case 'l':
  421.                         key = TK_l;
  422.                         break;
  423.                 case 'm':
  424.                         key = TK_m;
  425.                         break;
  426.                 case 'n':
  427.                         key = TK_n;
  428.                         break;
  429.                 case 'o':
  430.                         key = TK_o;
  431.                         break;
  432.                 case 'p':
  433.                         key = TK_p;
  434.                         break;
  435.                 case 'q':
  436.                         key = TK_q;
  437.                         break;
  438.                 case 'r':
  439.                         key = TK_r;
  440.                         break;
  441.                 case 's':
  442.                         key = TK_s;
  443.                         break;
  444.                 case 't':
  445.                         key = TK_t;
  446.                         break;
  447.                 case 'u':
  448.                         key = TK_u;
  449.                         break;
  450.                 case 'v':
  451.                         key = TK_v;
  452.                         break;
  453.                 case 'w':
  454.                         key = TK_w;
  455.                         break;
  456.                 case 'x':
  457.                         key = TK_x;
  458.                         break;
  459.                 case 'y':
  460.                         key = TK_y;
  461.                         break;
  462.                 case 'z':
  463.                         key = TK_z;
  464.                         break;
  465.                 case 'A':
  466.                         key = TK_A;
  467.                         break;
  468.                 case 'B':
  469.                         key = TK_B;
  470.                         break;
  471.                 case 'C':
  472.                         key = TK_C;
  473.                         break;
  474.                 case 'D':
  475.                         key = TK_D;
  476.                         break;
  477.                 case 'E':
  478.                         key = TK_E;
  479.                         break;
  480.                 case 'F':
  481.                         key = TK_F;
  482.                         break;
  483.                 case 'G':
  484.                         key = TK_G;
  485.                         break;
  486.                 case 'H':
  487.                         key = TK_H;
  488.                         break;
  489.                 case 'I':
  490.                         key = TK_I;
  491.                         break;
  492.                 case 'J':
  493.                         key = TK_J;
  494.                         break;
  495.                 case 'K':
  496.                         key = TK_K;
  497.                         break;
  498.                 case 'L':
  499.                         key = TK_L;
  500.                         break;
  501.                 case 'M':
  502.                         key = TK_M;
  503.                         break;
  504.                 case 'N':
  505.                         key = TK_N;
  506.                         break;
  507.                 case 'O':
  508.                         key = TK_O;
  509.                         break;
  510.                 case 'P':
  511.                         key = TK_P;
  512.                         break;
  513.                 case 'Q':
  514.                         key = TK_Q;
  515.                         break;
  516.                 case 'R':
  517.                         key = TK_R;
  518.                         break;
  519.                 case 'S':
  520.                         key = TK_S;
  521.                         break;
  522.                 case 'T':
  523.                         key = TK_T;
  524.                         break;
  525.                 case 'U':
  526.                         key = TK_U;
  527.                         break;
  528.                 case 'V':
  529.                         key = TK_V;
  530.                         break;
  531.                 case 'W':
  532.                         key = TK_W;
  533.                         break;
  534.                 case 'X':
  535.                         key = TK_X;
  536.                         break;
  537.                 case 'Y':
  538.                         key = TK_Y;
  539.                         break;
  540.                 case 'Z':
  541.                         key = TK_Z;
  542.                         break;
  543.             }
  544.         }
  545.         
  546.         if(KeyDownFunc)
  547.         {
  548.             (*KeyDownFunc)(key,mask);
  549.         }
  550.         if(DisplayFunc)
  551.         {
  552.             (*DisplayFunc)();
  553. #ifndef FX
  554.             Draw(Bound);
  555. #endif /* not FX */
  556.         }
  557.         be_app->ObscureCursor();
  558.         BView::KeyDown(bytes, numBytes);
  559.         return;
  560.  
  561.     };    
  562.     
  563.     void KeyUp(const char *bytes, int32 numBytes)
  564.     {
  565.         BView::KeyUp(bytes, numBytes);
  566.         return;
  567.     };
  568.     
  569.     void MessageReceived(BMessage *msg)
  570.     {
  571.         uint32 buttons = NULL;
  572.         ulong mask;
  573.         BPoint pos;
  574.         
  575.         the_view->GetMouse(&pos,&buttons);
  576.         
  577.         switch (msg->what)
  578.         {
  579.             case B_MOUSE_UP:
  580.                 if(MouseUpFunc)
  581.                 {
  582.                     mask = 0;
  583.                     if (buttons & B_PRIMARY_MOUSE_BUTTON) {
  584.                             mask |= TK_LEFTBUTTON;
  585.                     }
  586.                     if (buttons & B_SECONDARY_MOUSE_BUTTON) {
  587.                         mask |= TK_MIDDLEBUTTON;
  588.                     }
  589.                     if (buttons & B_TERTIARY_MOUSE_BUTTON) {
  590.                         mask |= TK_RIGHTBUTTON;
  591.                     }
  592.  
  593.                     (*MouseUpFunc)(x_pos,y_pos,(GLenum)mask);
  594.                     if( DisplayFunc)
  595.                     {
  596.                         (*DisplayFunc)();
  597.                             the_view->Draw(Bounds());
  598.                     }    
  599.                 }
  600.                 break;
  601.                 
  602.             default:
  603.                 BView::MessageReceived(msg);
  604.                 break;
  605.         }
  606.     };
  607.     
  608.     void Draw(BRect frame)
  609.     {
  610. #ifndef FX
  611.         DrawBitmap(the_bitmap,BPoint(0,0));
  612. #endif /* not FX */
  613.     };                    
  614. };
  615.  
  616. class MesaWindow : public BWindow 
  617. {
  618.     public:
  619.     
  620.     MesaWindow(int width, int height, const char* name):BWindow(BRect(0,0,width-1,height-1),name,B_TITLED_WINDOW, B_NOT_RESIZABLE | B_NOT_ZOOMABLE)
  621.     {
  622. #ifndef FX
  623.         MoveTo(windInfo.x,windInfo.y);
  624. #else /* FX */
  625.         BRect scr;
  626.         {
  627.         BScreen s; // local so it is destroyed quickly
  628.         scr = s.Frame();
  629.         }
  630.         MoveTo(0, 0);
  631.         ResizeTo(width = scr.bottom, height = scr.right);
  632. #endif /* FX */
  633.         Lock();
  634.         AddChild(the_view = new MesaView(BRect(0, 0, width, height)));
  635.         the_view->MakeFocus();
  636.         SetPreferredHandler((BHandler *)the_view);
  637.         Unlock();    
  638.     };
  639.     
  640.     void DispatchMessage(BMessage *msg, BHandler *hdl)
  641.     {
  642.         uint32 buttons = NULL;
  643.         ulong mask;
  644.         BPoint pos;
  645.         
  646.         the_view->GetMouse(&pos,&buttons);
  647.         
  648.         switch (msg->what)
  649.         {
  650.             case B_MOUSE_UP:
  651.                 the_view->MessageReceived(msg);
  652.                 break;
  653.                 
  654.             default:
  655.                 BWindow::DispatchMessage(msg, hdl);
  656.                 break;
  657.         }
  658.     };
  659.     
  660.     
  661.     void MessageReceived(BMessage *msg)
  662.     {
  663.         switch(msg->what)
  664.         {    
  665.             case    MSG_REDRAW:
  666. #ifndef FX
  667.                     the_view->DrawBitmap(the_bitmap,BPoint(0,0));
  668. #endif /* not FX */
  669.                        if (IdleFunc)
  670.                     {
  671.                         (*IdleFunc)();
  672.                         PostMessage(MSG_REDRAW);
  673.                     }
  674.                     break;
  675.             case B_MOUSE_UP:
  676.                     DispatchMessage(msg, the_view);
  677.                     break;
  678.             default:
  679.                     BWindow::MessageReceived(msg);
  680.                     break;
  681.         }
  682.     };
  683.     
  684.     bool QuitRequested(void)
  685.     {
  686.         be_app->PostMessage(B_QUIT_REQUESTED);
  687.         return TRUE;
  688.     };
  689. };
  690.  
  691. class MesaApp : public BApplication 
  692. {
  693.     public:
  694.     
  695.     MesaApp(char *sig):BApplication(sig)
  696.     {
  697.         the_window = NULL;
  698. #ifndef FX
  699.         the_bitmap = NULL;
  700. #endif /* not FX */
  701.     };    
  702.     
  703.     void ReadyToRun(void)
  704.     {
  705.         the_window = new MesaWindow(windInfo.width,windInfo.height, name);
  706.         the_window->Show();
  707.         
  708.         if(ExposeFunc)
  709.         {
  710.             (*ExposeFunc)(windInfo.width,windInfo.height);
  711.         }        
  712.         if(ReshapeFunc)
  713.         {
  714.             (*ReshapeFunc)(windInfo.width,windInfo.height);
  715.         }
  716.         if( DisplayFunc)
  717.         {
  718.             (*DisplayFunc)();
  719.         }
  720.         the_window->PostMessage(MSG_REDRAW);
  721.     };
  722.  
  723.     bool QuitRequested(void)
  724.     {
  725.         if (BApplication::QuitRequested()) 
  726.         {
  727. #ifndef FX
  728.             if (the_bitmap)    delete the_bitmap;
  729.             if (ctx) OSMesaDestroyContext( ctx );
  730. #else /* FX */
  731.             if (ctx) fxMesaDestroyContext( ctx );
  732. #endif /* FX */
  733.             return TRUE;
  734.         }
  735.         return FALSE;
  736.     };
  737.             
  738.     void AboutRequested(void)
  739.     {
  740.         char str[256];
  741.         sprintf(str, ", ported by Tinic Urou\n<5uro@informatik.uni-hamburg.de>\nFreely distributable.");
  742.         BAlert *the_alert = new BAlert("", str, "OK");
  743.         the_alert->Go();
  744.     };
  745. };
  746.  
  747.  
  748. /***************************************************************
  749.  *                                                             *
  750.  *  Exported Functions go here                                 *
  751.  *                                                             *
  752.  ***************************************************************/
  753.  
  754. void tkErrorPopups(GLboolean bEnable)
  755. {
  756. }
  757.  
  758. void tkCloseWindow(void)
  759. {
  760. }
  761.  
  762. void tkExec(void)
  763. {
  764.     the_app->Run();
  765.     delete the_app;
  766.     exit(0);
  767. }
  768.  
  769. void tkExposeFunc(void (*Func)(int, int))
  770. {
  771.     ExposeFunc = Func;
  772. }
  773.  
  774. void tkReshapeFunc(void (*Func)(GLsizei, GLsizei))
  775. {
  776.     ReshapeFunc = Func;
  777. }
  778.  
  779. void tkDisplayFunc(void (*Func)(void))
  780. {
  781.     DisplayFunc = Func;
  782. }
  783.  
  784. void tkKeyDownFunc(GLenum (*Func)(int, GLenum))
  785. {
  786.     KeyDownFunc = Func;
  787. }
  788.  
  789. void tkMouseDownFunc(GLenum (*Func)(int, int, GLenum))
  790. {
  791.     MouseDownFunc = Func;
  792. }
  793.  
  794. void tkMouseUpFunc(GLenum (*Func)(int, int, GLenum))
  795. {
  796.     MouseUpFunc = Func;
  797. }
  798.  
  799. void tkMouseMoveFunc(GLenum (*Func)(int, int, GLenum))
  800. {
  801.     MouseMoveFunc = Func;
  802. }
  803.  
  804. void tkIdleFunc(void (*Func)(void))
  805. {
  806.     IdleFunc = Func;
  807. }
  808.  
  809. void tkInitPosition(int x, int y, int width, int height)
  810. {
  811.    windInfo.bDefPos = FALSE;
  812.    windInfo.x = x;
  813.    windInfo.y = y;
  814.    windInfo.width = width;
  815.    windInfo.height = height;
  816. }
  817.  
  818. void tkInitDisplayMode(GLenum type)
  819. {
  820.     windInfo.type = type;
  821. }
  822.  
  823. void tkInitDisplayModePolicy(GLenum type)
  824. {
  825.     windInfo.dmPolicy = type;
  826. }
  827.  
  828. GLenum tkInitDisplayModeID(GLint ipfd)
  829. {
  830.     windInfo.ipfd = ipfd;
  831.     return GL_TRUE;
  832. }
  833.  
  834. // needed to set current directory for Tracker launches...
  835. void SetCurrentDirectory() 
  836.     app_info info; 
  837.     BPath path; 
  838.     be_app->GetAppInfo(&info); 
  839.     BEntry entry(&info.ref); 
  840.     entry.GetPath(&path); 
  841.     path.GetParent(&path); 
  842.     chdir(path.Path()); 
  843.  
  844.  
  845. GLenum tkInitWindowAW(char *title, bool bUnicode)
  846. {
  847. #ifdef FX
  848. #define NUM_RES 3
  849.  
  850.    static GrScreenResolution_t res[NUM_RES+1] = {
  851.       GR_RESOLUTION_512x384,
  852.       GR_RESOLUTION_640x480,
  853.       GR_RESOLUTION_800x600,
  854.       GR_RESOLUTION_NONE};
  855.    static int xres[NUM_RES]={512,640,800};
  856.    static int yres[NUM_RES]={384,480,600};
  857.    int i;
  858.    GLuint window;
  859.    GLint attribs[100];
  860. #else    /* FX */
  861.     int32 realw;
  862.     color_space type;
  863. #endif /* FX */
  864.  
  865.      windInfo.title=title;
  866.      name = title;
  867.     
  868.     // What a hack!
  869.     the_app = new MesaApp("application/x-vnd.MesaApp");
  870.     SetCurrentDirectory();
  871. #ifndef FX
  872.     switch(windInfo.type)
  873.     {
  874.         case    TK_RGB:
  875.                 type=B_RGB_32_BIT;
  876.                 break;
  877.         case    TK_INDEX:
  878.                 type=B_COLOR_8_BIT;
  879.                 break;
  880.     }
  881.  
  882.     // other mode does not work now
  883.     type=B_RGB_32_BIT;
  884.     windInfo.type=(GLenum)TK_RGB;
  885.  
  886.     the_bitmap = new BBitmap(BRect(0, 0, windInfo.width,windInfo.height),type);
  887.     bits = (uchar *)the_bitmap->Bits();
  888.  
  889.     switch(type)
  890.     {
  891.         case    B_RGB_32_BIT:
  892.                 realw=the_bitmap->BytesPerRow()/sizeof(int32);
  893.                 // The OSMESA_BGRA in the OSMesaCreateContext needs to be changed for the x86 version.
  894.                 // I think it's an endian thing. See osmesa.h for the different modes.
  895.                 ctx = OSMesaCreateContext( (GLenum)OSMESA_BGRA, NULL );
  896.                 tmpout=0;
  897.                 memset(bits,0,the_bitmap->BytesPerRow()*windInfo.height);
  898.                 OSMesaMakeCurrent( ctx, bits, GL_UNSIGNED_BYTE,realw,windInfo.height);
  899.                 break;
  900.         case    B_COLOR_8_BIT:
  901.                 realw=the_bitmap->BytesPerRow();
  902.                 ctx = OSMesaCreateContext( GL_COLOR_INDEX, NULL );
  903.                 tmpout=new uchar[the_bitmap->BytesPerRow()*windInfo.height];
  904.                 memset(tmpout,0,the_bitmap->BytesPerRow()*windInfo.height);
  905.                 OSMesaMakeCurrent( ctx, tmpout, GL_UNSIGNED_BYTE,realw,windInfo.height);
  906.                 break;
  907.     }
  908.     OSMesaPixelStore( OSMESA_Y_UP, 0 );
  909. #else /* FX */
  910.    /* Build fxMesa attribute list */
  911.    i = 0;
  912.    if (TK_IS_DOUBLE(windInfo.type)) {
  913.       attribs[i] = FXMESA_DOUBLEBUFFER;
  914.       i++;
  915.    }
  916.    if (TK_HAS_DEPTH(windInfo.type)) {
  917.       attribs[i] = FXMESA_DEPTH_SIZE;
  918.       i++;
  919.       attribs[i] = 1;
  920.       i++;
  921.    }
  922.    if (TK_HAS_ALPHA(windInfo.type)) {
  923.       attribs[i] = FXMESA_ALPHA_SIZE;
  924.       i++;
  925.       attribs[i] = 1;
  926.       i++;
  927.    }
  928.    if (TK_HAS_ACCUM(windInfo.type)) {
  929.       attribs[i] = FXMESA_ACCUM_SIZE;
  930.       i++;
  931.       attribs[i] = 1;
  932.       i++;
  933.    }
  934.    if (TK_HAS_STENCIL(windInfo.type)) {
  935.       attribs[i] = FXMESA_STENCIL_SIZE;
  936.       i++;
  937.       attribs[i] = 1;
  938.       i++;
  939.    }
  940.    attribs[i] = FXMESA_NONE;  /* end of list */
  941.  
  942.    /* find smallest screen size >= requested size */
  943.    for(i=0;i<NUM_RES;i++)
  944.       if((windInfo.width<=xres[i]) && (windInfo.height<=yres[i]))
  945.          break;
  946.  
  947.    if(i == NUM_RES)
  948.       i = NUM_RES-1;  /* largest possible */
  949.    window = 0;
  950.  
  951.    windInfo.width = xres[i];
  952.    windInfo.height = yres[i];
  953.  
  954.    ctx = fxMesaCreateContext(window, res[i], GR_REFRESH_75Hz, attribs);
  955.    if(!ctx) {
  956.       return(GL_FALSE);
  957.    }
  958.    fxMesaMakeCurrent(ctx);
  959. #endif /* FX */
  960.  
  961.     return GL_TRUE;
  962. }
  963.  
  964. GLenum tkInitWindow(char *title)
  965. {
  966.     return tkInitWindowAW(title, FALSE);
  967. }
  968.  
  969.  
  970. /******************************************************************************/
  971.  
  972. /*
  973.  * You cannot just call DestroyWindow() here.  The programs do not expect
  974.  * tkQuit() to return;  DestroyWindow() just sends a WM_DESTROY message
  975.  */
  976.  
  977. void tkQuit(void)
  978. {
  979.     be_app->PostMessage(B_QUIT_REQUESTED);
  980. }
  981.  
  982. /******************************************************************************/
  983.  
  984. void set_onecolor(int k, uint8 r, uint8 g, uint8 b)
  985. {
  986.     BScreen s;
  987.     idxlist[(k&0xff)]=s.IndexForColor(r,g,b);
  988. }
  989.  
  990. void tkSetOneColor(int index, float r, float g, float b)
  991. {
  992.     uint8 R=r*255.0,G=g*255.0,B=b*255.0;
  993.     set_onecolor(index,R,G,B);
  994. }
  995.  
  996. void tkSetFogRamp(int density, int startIndex)
  997. {
  998.     int n, i, j, k, intensity, fogValues, colorValues;
  999.  
  1000.     fogValues = 1 << density ;
  1001.     colorValues = 1 << startIndex ;
  1002.     for( i = 0 ; i < colorValues; i++ ) {
  1003.         for( j = 0 ; j < fogValues; j++ ) {
  1004.             k = i * fogValues + j;
  1005.  
  1006.             intensity = i * fogValues + j * colorValues;
  1007.  
  1008.             if( intensity > 0xff )
  1009.                 intensity = 0xff;
  1010.  
  1011.             set_onecolor(k,intensity,intensity,intensity);
  1012.         }
  1013.     }
  1014. }
  1015.  
  1016. void tkSetGreyRamp(void)
  1017. {
  1018.     int Count,i;
  1019.     float intensity;
  1020.     Count = tkGetColorMapSize();
  1021.     for( i = 0 ; i < Count ; i++ )
  1022.     {
  1023.         intensity = (float)(((double)i / (double)(Count-1)) * (double)255.0 + (double)0.5);
  1024.         set_onecolor(i,intensity,intensity,intensity);
  1025.     }
  1026. }
  1027.  
  1028. void tkSetRGBMap( int Size, float *Values )
  1029. {
  1030.     for (int i=0; i<Size; i++) tkSetOneColor(i,Values[i*3],Values[i*3+1],Values[i*3+2]);
  1031. }
  1032.  
  1033. void tkSwapBuffers(void)
  1034. {
  1035. #ifdef FX
  1036.     fxMesaSwapBuffers();
  1037. #endif /* FX */
  1038. }
  1039.  
  1040. GLint tkGetColorMapSize(void)
  1041. {
  1042.     return 256;
  1043. }
  1044.  
  1045. void tkGetMouseLoc(int *x, int *y)
  1046. {
  1047.     *x = x_pos;
  1048.     *y = y_pos;
  1049. }
  1050.  
  1051. GLenum tkGetDisplayModePolicy(void)
  1052. {
  1053.     return windInfo.dmPolicy;
  1054. }
  1055.  
  1056. GLint tkGetDisplayModeID(void)
  1057. {
  1058.     return windInfo.ipfd;
  1059. }
  1060.  
  1061. GLenum tkGetDisplayMode(void)
  1062. {
  1063.     return windInfo.type;
  1064. }
  1065.